package main.smarthouse.beans;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.event.CellEditEvent;

import smarthouse.dto.HouseDTO;
import smarthouse.dto.PrivilegeDTO;
import smarthouse.dto.UserDTO;
import smarthouse.ejb.HouseDAORemote;
import smarthouse.ejb.PrivilegeDAORemote;
import smarthouse.ejb.UserDAORemote;

/**
 * @author Sergiu
 *
 */
@SessionScoped
@ManagedBean(name = "userViewBean")
public class UserViewBean {

	@EJB
	private UserDAORemote userDAO;

	@EJB
	private PrivilegeDAORemote privilegeDAO;

	@EJB
	private HouseDAORemote houseDAO;

	private UserDTO loggedUser;

	private HouseDTO house;

	private List<UserDTO> houseUsers = new ArrayList<>();

	private int selectedUser;

	private PrivilegeDTO selectedPrivilege;

	private UserDTO newUser = new UserDTO();

	private PrivilegeDTO newPrivilege = new PrivilegeDTO();

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public PrivilegeDTO getNewPrivilege() {
		return newPrivilege;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public void setNewPrivilege(PrivilegeDTO newPrivilege) {
		this.newPrivilege = newPrivilege;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public UserDTO getNewUser() {
		return newUser;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public void setNewUser(UserDTO newUser) {
		this.newUser = newUser;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public UserDTO getLoggedUser() {
		loggedUser = (UserDTO) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("loggedUser");
		return loggedUser;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public void setLoggedUser(UserDTO loggedUser) {
		this.loggedUser = loggedUser;
	}

	/**
	 * Default constructor
	 */
	public UserViewBean() {
		house = (HouseDTO) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("selectedHouse");

	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public List<UserDTO> getHouseUsers() {
		if (house.getUsers() != null && houseUsers.isEmpty()) {
			for (int i : house.getUsers()) {

				houseUsers.add(userDAO.getUserbyId(i));
			}
		}

		return houseUsers;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public void setHouseUsers(List<UserDTO> houseUsers) {
		this.houseUsers = houseUsers;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public HouseDTO getHouse() {
		return house;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public void setHouse(HouseDTO house) {
		this.house = house;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public int getSelectedUser() {
		return selectedUser;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public void setSelectedUser(int selectedUser) {
		this.selectedUser = selectedUser;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public PrivilegeDTO getSelectedPrivilege() {
		return selectedPrivilege;
	}

	/**
	 * Getter/Setter
	 * 
	 * @return
	 */
	public void setSelectedPrivilege(PrivilegeDTO selectedPrivilege) {
		this.selectedPrivilege = selectedPrivilege;
	}

	/**
	 * Toggle selected user for privilege table
	 * 
	 * @param e
	 */
	public void toggleSelectedUser(ActionEvent e) {

		selectedUser = (Integer) e.getComponent().getAttributes().get("userId");
		selectedPrivilege = privilegeDAO.getPrivilegebyId(selectedUser);
	}

	/**
	 * Remove use from house
	 * 
	 * @param e
	 */
	public void removeUser(ActionEvent e) {
		int removedUser = (int) e.getComponent().getAttributes()
				.get("deleteId");

		privilegeDAO.removePrivilege(removedUser);
		userDAO.removeUser(removedUser);
		house.getUsers().remove(userDAO.getLastUserId());
		houseUsers = new ArrayList<>();
		if (selectedUser == removedUser) {
			selectedUser = 0;
			selectedPrivilege = null;
		}
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
				.put("selectedHouse", house);
	}

	/**
	 * Update user information
	 * 
	 * @param e
	 */
	public void updateUser(CellEditEvent e) {
		int i = e.getRowIndex();
		UserDTO u = houseUsers.get(i);

		userDAO.updateUser(u);

	}

	/**
	 * Update privilege information
	 */
	public void updatePrivileges() {

		privilegeDAO.updatePrivilege(selectedPrivilege);
	}

	/**
	 * Adding user and privilege to database
	 * 
	 * @return
	 */
	public String submitUser() {

		newUser.setHouse(house.getHouseId());

		privilegeDAO.addPrivilege(newPrivilege);
		userDAO.addUser(newUser);

		house.getUsers().add(userDAO.getLastUserId());
		houseUsers = new ArrayList<>();
		return "userView.xhtml?faces-redirect=true";
	}

}
